from django.http import JsonResponse
from django.views import View


def login_required(view_func):
    def wrapper(request, *args, **kwargs):
        if not request.user.is_authenticated:
            # 用户未登录
            return JsonResponse({'errno': 4101,
                                 'errmsg': '未登录!'})
        else:
            # 用户已登录
            return view_func(request, *args, **kwargs)

    return wrapper


class LoginRequiredMixin:
    """登录验证的Mixin扩展类"""
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(**initkwargs)

        return login_required(view)
